<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="问题原因" prop="problemCause">
            <!-- <Tinymce ref="tinymce" v-model="formItem.problemCause" :height="200" menubar="false" /> -->
            <el-input v-model="formItem.problemCause" placeholder="请输入内容" :rows="10" :maxlength="2047" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="解决方案" prop="plan">
            <!-- <Tinymce ref="tinymce" v-model="formItem.plan" :height="200" menubar="false" /> -->
            <el-input v-model="formItem.plan" placeholder="请输入内容" :rows="10" :maxlength="2047" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="附件">
            <el-upload
              class="upload-demo"
              :headers="headers"
              :action="importUrl"
              :file-list="formItem.attachments"
              :before-upload="handleBeforeUpLoad"
              :on-success="handleSuccess"
              :on-error="handleError"
              :on-remove="handleRemove"
              :show-file-list="true"
            >
              <el-button type="primary" icon="el-icon-upload2">上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="处理结果" required prop="result">
            <el-radio v-for="(item) in result" :key="item.value" v-model="formItem.result" :label="item.value" @change="change">{{ item.label }}</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="showCause" :gutter="40">
        <el-col :span="24">
          <el-form-item label="未完成原因" prop="unfinishedCause">
            <el-input v-model="formItem.unfinishedCause" placeholder="请输入未完成原因" type="textarea" :rows="3" :autosize="true" resize="none" />
          </el-form-item>
        </el-col>
      </el-row>
      <div style="text-align: right;">
        <el-form-item>
          <el-button type="warning" @click="ok('temporary')">暂存</el-button>
          <el-button type="primary" @click="ok('submit')">提交</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  importUrl,
  detail,
  deleteAttachmentByFsId
} from '@neu/dcom-ui/api/problemHandling'
// import Tinymce from '@neu/biz-core/components/Tinymce'
import { getToken } from '@neu/biz-core/utils/auth'
const formItem = {
  problemId: '',
  problemCause: '',
  plan: '',
  attachments: [],
  unfinishedCause: '',
  result: true,
  status: '',
  workOrderNum: '',
  taskId: ''
}
export default {
  name: 'ProblemHandling',
  components: {
    // Tinymce
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      /** 上传路径 */
      importUrl: importUrl,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      /** 处理结果 */
      result: [{ value: true, label: '已完成' }, { value: false, label: '未完成' }],
      /** 是否显示用户选择对话框 */
      userSelDialogFormVisible: false,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      showCause: false,
      // 表单验证规则
      rules: {
        result: [{
          required: true,
          message: '处理结果不能为空',
          trigger: 'change'
        }],
        problemCause: [{
          required: true,
          message: '问题原因不能为空',
          trigger: 'blur'
        }],
        plan: [{
          required: true,
          message: '解决方案不能为空',
          trigger: 'blur'
        }],
        unfinishedCause: [{
          required: true,
          message: '未完成原因不能为空',
          trigger: 'blur'
        }]

      }
    }
  },
  watch: {},
  created() {
    this.fetch()
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
          this.showCause = !this.formItem.result
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    ok(value) {
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            this.formItem.workOrderNum = this.workOrderNum
            this.formItem.taskId = this.taskId
            this.formItem.status = value
            create(this.formItem).then(res => {
              if (res.status === 'success') {
                this.$emit('cancelNewOrderForm', 'refresh')
              }
            })
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '文件上传成功',
          type: 'success'
        })
        const obj = {
          fsId: response.data.fsId,
          name: response.data.name
        }
        const fsIdArr = this.formItem.attachments.map(t => t.fsId)
        if (!fsIdArr.includes(obj.fsId)) {
          this.formItem.attachments.push(obj)
        } else {
          fileList.pop()
        }
      }
    },
    handleError(err, file, fileList) {
      console.log(err)
    },
    handleRemove(file, fileList) {
      deleteAttachmentByFsId(file.fsId, this.id).then(res => {
        if (res.status === 'failure') {
          this.$message({
            showClose: true,
            message: res.data,
            type: 'error'
          })
        } else {
          this.formItem.attachment.splice(this.formItem.attachment.indexOf(file), 1)
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
        }
      })
    },
    handleBeforeUpLoad(file) {},
    change(value) {
      if (value) {
        this.showCause = false
      } else {
        this.showCause = true
      }
    }
  }
}
</script>
